package com.junyidao.game.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junyidao.game.Exception.BusinessException;
import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.GroupConstant;
import com.junyidao.game.mapper.TChannelUserRelaMapper;
import com.junyidao.game.mapper.TGroupChannelRelaMapper;
import com.junyidao.game.mapper.TGroupMapper;
import com.junyidao.game.mapper.TGroupUserRelaMapper;
import com.junyidao.game.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2016/8/14.
 */
@Service
@Transactional
public class GroupService {

    @Autowired
    private TGroupMapper groupMapper;

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private TGroupChannelRelaMapper groupChannelRelaMapper;

    @Autowired
    private TGroupUserRelaMapper groupUserRelaMapper;

    @Autowired
    private TGroupUserRelaMapper tGroupUserRelaMapper;

    @Autowired
    private TGroupChannelRelaMapper tGroupChannelRelaMapper;

    public Map<String, Object> selectGroup(TGroupVo vo) {
        PageHelper.startPage(vo.getPage(), vo.getRows());
        List<TGroup> list = groupMapper.selectGroup(vo);
        PageInfo pageInfo = new PageInfo(list);
        Map<String, Object> ret = new HashMap<>();
        ret.put("list", list);
        ret.put("count", pageInfo.getTotal());
        return ret;
    }

    public TGroup selectGroupByGroupId(Integer id) {
        TGroup group = new TGroup();
        group.setGroupId(id);
        List<TGroup> list = groupMapper.select(group);
        return list.size() > 0 ? list.get(0) : null;
    }

    public void activate(String ids, String groupStatus) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", idList);
        map.put("groupStatus", groupStatus);
        groupMapper.activateGroup(map);
    }

    public Integer addGroup(TGroup group) {
        TGroup group1 = new TGroup();
        group1.setGroupName(group.getGroupName());
        List<TGroup> groupList = groupMapper.select(group1);
        if (groupList.size() > 0) {
            BusinessException.throwException(1, "群组已存在，请修改群组名");
        }
        Integer id = sequenceService.getNextSequence("GROUP_ID");
        group.setGroupId(id);
        group.setCreateTime(new Date());
        group.setGroupStatus(BaseConstant.STATUS_NORMAL);
        groupMapper.insert(group);
        return group.getGroupId();
    }

    public void addChannel(String ids, Integer groupId) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        TGroup par = new TGroup();
        par.setGroupId(groupId);
        TGroup group = groupMapper.selectOne(par);
        if (group == null) {
            BusinessException.throwException(1, "群组不存在");
        }
        if (!group.getGroupType().equals(GroupConstant.GROUP_TYPE_CHANNEL)) {
            BusinessException.throwException(1, "该群组只能添加渠道");
        }
        for (Integer channelId : idList) {
            TGroupChannelRela rela = new TGroupChannelRela();
            Integer id = sequenceService.getNextSequence("GCR_ID");
            rela.setGcrId(id);
            rela.setGroupId(groupId);
            rela.setChannelId(channelId);
            rela.setAddDate(new Date());
            rela.setGcrStatus(BaseConstant.STATUS_NORMAL);
            groupChannelRelaMapper.insert(rela);
        }
    }

    public void addUser(String ids, Integer groupId) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        TGroup par = new TGroup();
        par.setGroupId(groupId);
        TGroup group = groupMapper.selectOne(par);
        if (group == null) {
            BusinessException.throwException(1, "群组不存在");
        }
        if (!group.getGroupType().equals(GroupConstant.GROUP_TYPE_USER)) {
            BusinessException.throwException(1, "该群组只能添加账号");
        }
        for (Integer uid : idList) {
            TGroupUserRela rela = new TGroupUserRela();
            Integer id = sequenceService.getNextSequence("GUR_ID");
            rela.setGurId(id);
            rela.setGroupId(groupId);
            rela.setUid(uid);
            rela.setAddDate(new Date());
            rela.setGurStatus(BaseConstant.STATUS_NORMAL);
            groupUserRelaMapper.insert(rela);
        }
    }

    public void updatePartByPrimaryKey(TGroup group) {
        groupMapper.updatePartByPrimaryKey(group);
    }

    public void delUser(String ids, Integer groupId) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("groupId", groupId);
        map.put("uids", idList);
        tGroupUserRelaMapper.delGroupUser(map);
    }

    public void deChannel(String ids, Integer groupId) {
        List<Integer> idList = JSONArray.parseArray(ids, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("groupId", groupId);
        map.put("ids", idList);
        tGroupChannelRelaMapper.delGroupChannel(map);
    }
}
